package com.hwua.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hwua.dao.IUserDao;
import com.hwua.dao.Impl.UserDaoImpl;
import com.hwua.entity.User;
import com.hwua.service.IUserService;
import com.hwua.service.Impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/lr")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String param = request.getParameter("param");
        String codes = request.getParameter("code");
        IUserDao userDao=new UserDaoImpl();
        IUserService userService=new UserServiceImpl();
        Map<String,Object> map=new HashMap<>();
        //登录
        if(param.equals("login")){
            String uname = request.getParameter("uname");
            String pwd = request.getParameter("pwd");

            //先比较验证码
            HttpSession session = request.getSession(false);
            String vcode = (String) session.getAttribute("code");//从session取出验证码
            System.out.println(vcode);
            System.out.println(codes);
           /* session.removeAttribute("code");//取出验证码就失效,只能用一次*/

            //比对验证码
            if (vcode == null || !vcode.equals(codes)) {
                map.put("success",false);
                map.put("info","验证码错误");
            }else{
                User user=new User();
                user.setUname(uname);
                user.setPwd(pwd);
                try {
                    user= userService.login(user);
                    System.out.println(user);
                    if(user!=null){
                        session.setAttribute("user",user);
                        map.put("success",true);
                        map.put("info","");
                    }else{
                        map.put("success",false);
                        map.put("info","账户密码错误");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        //注册
         if(param.equals("register")) {
            System.out.println("haha");
            User user=new User();
            String uname = request.getParameter("uname");
            System.out.println(uname);
            String pwd = request.getParameter("pwd");
            String affim = request.getParameter("affim");
            String sex = request.getParameter("sex");
            String birth = request.getParameter("birth");
            String idcard = request.getParameter("idcard");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            HttpSession session = request.getSession(false);
            String vcode = (String) session.getAttribute("code");
            session.removeAttribute("code");
            if (vcode == null || !vcode.equals(codes)) {
                map.put("success", false);
            } else {
                try {
                    User use = userDao.query(uname);
                    if(use!=null && !pwd.equals(affim)){
                       map.put("success",false);
                    }else{
                        //string转int
                        System.out.println("hahaha");
                        user.setUname(uname);
                        user.setPwd(pwd);
                        user.setSex(Integer.parseInt(sex));//?
                        /*SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                        String format = simpleDateFormat.format(birth);*/
                        user.setBirthday(birth);
                        user.setIdcard(idcard);
                        user.setEmail(email);
                        user.setAddress(address);
                        user.setMobile(phone);
                        user.setUtype(1);
                        boolean register = userService.register(user);
                        System.out.println(register);
                        map.put("success",register);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        String jsonStr=JSON.toJSONString(map, SerializerFeature.PrettyFormat);
        response.getWriter().write(jsonStr);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
